package littlepicture.welcome
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	public class WelcomeScreen  extends Loader{
		
		[Embed(source="welcome.jpg", mimeType="application/octet-stream")]
		public var WelcomeScreenGraphic:Class;
		public var timer:Timer;
		private var timeAutoClose:int = 100;
		private var fadeInRate:Number  = .02;
		private var fadeOutRate:Number = .05;
		
		public function WelcomeScreen(){
			visible = false;
			alpha = 0;
			
			timer = new Timer(1);
            timer.addEventListener(TimerEvent.TIMER, updateView);
			timer.start();
			
			loadBytes( new WelcomeScreenGraphic() as ByteArray );
		}
		
		public function updateView(event:TimerEvent):void{
			if(alpha<1)
			  alpha+=fadeInRate;
			  
			 stage.addChild(this);
			 x = stage.stageWidth/2 - width/2;
			 y = stage.stageHeight/2 - height/2;
			 visible=true;
			 
			 if(timer.currentCount>timeAutoClose) 
			   closeScreen();	
		}
		
		public function closeScreen():void{
            timer.removeEventListener( TimerEvent.TIMER, updateView );
			timer.addEventListener( TimerEvent.TIMER, closeScreenFade );					
		}
		
		public function closeScreenFade( event:TimerEvent ):void
		{
			if( alpha > 0){
				alpha-=fadeOutRate;
			} else {
				timer.stop();
				parent.removeChild(this);
			}		
		}		
	}
}